﻿var Usageself = this;
var item = null;
var waitDialog = null;
Usageself.Wait = function () {
    waitDialog = SP.UI.ModalDialog.showWaitScreenWithNoClose(SP.Res.dialogLoading15);
    alert('Item updated!');
}

Usageself.CancelWait = function () {
    waitDialog.close();
}
Usageself.appWebContext = null;
Usageself.appWeb = null;
Usageself.itemCollection = null;
Usageself.list = null;
Usageself.currentUser = null;
var newUsageStatistic = null;
$(document).ready(function () {
   
    Usageself.appWebContext = new SP.ClientContext.get_current();
    Usageself.appWeb = Usageself.appWebContext.get_web();
    Usageself.list = Usageself.appWeb.get_lists().getByTitle("UsageStatistic");
    Usageself.currentUser = Usageself.appWeb.get_currentUser();
    Usageself.appWebContext.load(Usageself.list);
    Usageself.appWebContext.load(Usageself.currentUser);
    //var itemCreateInfo = new SP.ListItemCreationInformation();
    //var newUsageStatistic = Usageself.list.addItem(itemCreateInfo)
    //newUsageStatistic.set_item("User", Usageself.currentUser);
    //newUsageStatistic.set_item("StartTime", new Date().);
    //newUsageStatistic.set_item("Title", "dfsF");
    //newUsageStatistic.update();
    Usageself.appWebContext.executeQueryAsync(Usageself.GetList, Usageself.onError);
});


Usageself.GetList = function () {
    var itemCreateInfo = new SP.ListItemCreationInformation();
    newUsageStatistic = Usageself.list.addItem(itemCreateInfo)
    newUsageStatistic.set_item("User1", Usageself.currentUser);
    var Time = new Date();
    newUsageStatistic.set_item("StartTime", Time);
    newUsageStatistic.set_item("Title", Usageself.currentUser.get_title());
    newUsageStatistic.update();
    //item = Usageself.list.addItem(itemCreateInfo);
    //item.set_item("User", Usageself.currentUser);
    //item.set_item("StartTime", new Date().getTime());
    //item["Title"] = " ";
    //item["User"] = Usageself.currentUser;
    //item["StartTime"] = new Date().getTime();
    //item.update();
    Usageself.appWebContext.load(newUsageStatistic);
    Usageself.appWebContext.executeQueryAsync(Usageself.onsuccess, Usageself.onError);
}

Usageself.onError = function (err) {
    alert(err);
}
Usageself.onsuccess = function () {
    //alert("success");
    /*Usageself.list = Usageself.appWeb.get_lists().getByTitle("UsageStatistic").getItemById(item.get_id());
    Usageself.list.set_item("User", Usageself.currentUser);
    Usageself.list.set_item("StartTime", new Date().getTime());
    Usageself.appWebContext.executeQueryAsync(Usageself.onsuccess1, Usageself.onError);*/
}
Usageself.onsuccess1 = function () {
    //alert('Item updated!');
    CancelWait();

}
$(window).unload(function () {
    //item.set_item("EndTime", new Date().getTime());
    //item["EndTime"] = new Date().getTime();
    /*
    var Time = new Date();
    newUsageStatistic.set_item("EndTime", Time);
    newUsageStatistic.update();
    Usageself.appWebContext.load(newUsageStatistic);
    Usageself.appWebContext.executeQueryAsync(Usageself.onsuccess1, Usageself.onError);
    Usageself.Wait();*/

});